/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

import businessObjects.Cliente;
import daos.ClienteDAO;
import daos.exceptions.IllegalOrphanException;
import daos.exceptions.NonexistentEntityException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

/**
 *
 * @author williamprado
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class ClienteServices implements ClienteServicesLocal {

    @PersistenceUnit
    private EntityManagerFactory entityManagerFactory;
    private EntityManager entityManager;

    @PostConstruct
    public void initialize(){
    entityManager = entityManagerFactory.createEntityManager();
    }

    @PreDestroy
    public void cleanUp(){
        if(entityManager.isOpen()){
            entityManager.close();
        }
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    public void nuevoCliente(Cliente cliente) {
        ClienteDAO cd = new ClienteDAO();
        try {
            cd.create(cliente, entityManager);
        } catch (Exception ex) {
            Logger.getLogger(ClienteServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Cliente login(String login, String pass){
        ClienteDAO cd = new ClienteDAO();
        Cliente cliente = new Cliente();        
        try {
            cliente = cd.findCliente(login, entityManager);
        } catch (Exception ex) {
            Logger.getLogger(ClienteServices.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(cliente != null){
            //System.out.println(cliente.getPassword());
            //System.out.println(pass);
            if (cliente.getPassword().equals(pass)){
                return cliente;
            }else{return null;}
        }else{
            System.out.println("error");
            return null;}
    }

    public Cliente consultarCliente(String id) {
        ClienteDAO cd = new ClienteDAO();
        Cliente cliente = new Cliente();
        try {
            cliente = cd.findCliente(id, entityManager);
        } catch (Exception ex) {
            Logger.getLogger(ClienteServices.class.getName()).log(Level.SEVERE, null, ex);
        }
        return cliente;
    }

    public void actualizarCliente(Cliente cliente) {
        ClienteDAO cd = new ClienteDAO();
        try {
            cd.edit(cliente, entityManager);
        } catch (IllegalOrphanException ex) {
            Logger.getLogger(ClienteServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(ClienteServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ClienteServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void eliminarCliente(String id) {
        ClienteDAO cd = new ClienteDAO();
        try {
            cd.destroy(id, entityManager);
        } catch (IllegalOrphanException ex) {
            Logger.getLogger(ClienteServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(ClienteServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(ClienteServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }    
 
}
